package sunwul.hospital.hosp.controller.api;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sunwul.hospital.hosp.service.DepartmentService;
import sunwul.hospital.hosp.util.CheckUtil;
import sunwul.hospital.common.helper.HttpRequestHelper;
import sunwul.hospital.common.result.Result;
import sunwul.hospital.model.model.hosp.Department;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author sunwul
 * @date 2023/11/4 16:44:08
 * @description  对接service-manage模块
 */
@Api(tags = "科室信息管理")
@RestController
@RequestMapping("/api/hosp")
public class DepartmentApiController {

    @Autowired
    DepartmentService departmentService;

    @Autowired
    CheckUtil checkUtil;

    @ApiOperation(value = "分页获取科室信息")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        // 获取参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        Map<String, Object> departmentPage = departmentService.findDepartment(paramMap);
        return Result.ok(departmentPage);
    }

    @ApiOperation(value = "上传科室信息")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        // 获取参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 校验
        checkUtil.checkSignKey(paramMap);
        // 根据service-manage调用情况, manage上传为循环调用一个一个上传信息
        Department department = JSON.parseObject(JSON.toJSONString(paramMap),Department.class);
        /***
         * TODO 逻辑删除的问题 2023-11-05 sunwul
         * 由于逻辑删除的存在, 当存在数据为已逻辑删除的状态时, 再次向数据库中添加数据会出现重复添加的情况,
         * 当数据表设置了某些字段索引唯一时, 此时会出现添加报错
         * 因此在实际场景中的使用时, 设置某些数据库表的索引, 应当考虑到逻辑删除的存在
         */
        // 保存或更新
        QueryWrapper<Department> queryWrapper = new QueryWrapper();
        queryWrapper.eq("hoscode",department.getHoscode());
        queryWrapper.eq("depcode",department.getDepcode());
        Department dep = departmentService.getOne(queryWrapper);
        boolean flag;
        if (dep != null){
            flag = departmentService.update(department,queryWrapper);
        }else{
            flag = departmentService.save(department);
        }
        if (!flag) {
            return Result.fail();
        }
        return Result.ok();
    }

    @ApiOperation(value = "删除科室信息")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        // 获取参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 校验
        checkUtil.checkSignKey(paramMap);
        String depcode = (String) paramMap.get("depcode");
        String hoscode = (String) paramMap.get("hoscode");
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("depcode",depcode);
        wrapper.eq("hoscode",hoscode);
        boolean flag = departmentService.remove(wrapper);
        if (!flag){
            Result.fail();
        }
        return Result.ok();
    }
}
